package com.ruoyi.system.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.enums.EnterpriseStatusEnum;
import com.ruoyi.common.enums.PositionStatusEnum;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysPosition;
import com.ruoyi.system.service.ISysPositionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysEnterpriseMapper;
import com.ruoyi.system.domain.SysEnterprise;
import com.ruoyi.system.service.ISysEnterpriseService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 企业Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-03-09
 */
@Service
public class SysEnterpriseServiceImpl extends ServiceImpl<SysEnterpriseMapper ,SysEnterprise> implements ISysEnterpriseService
{
    @Autowired
    private SysEnterpriseMapper sysEnterpriseMapper;

    @Autowired
    private ISysPositionService positionService ;

    /**
     * 查询企业
     * 
     * @param id 企业主键
     * @return 企业
     */
    @Override
    public SysEnterprise selectSysEnterpriseById(Long id)
    {
        return sysEnterpriseMapper.selectSysEnterpriseById(id);
    }

    /**
     * 查询企业列表
     * 
     * @param sysEnterprise 企业
     * @return 企业
     */
    @Override
    public List<SysEnterprise> selectSysEnterpriseList(SysEnterprise sysEnterprise) {
        Long userId = SecurityUtils.getUserId();
        /**只有管理员才能查看全部信息*/
        if (!SecurityUtils.isAdmin(userId)) {
            sysEnterprise.setUserId(userId);
        }
        List<SysEnterprise> sysEnterprises = sysEnterpriseMapper.selectSysEnterpriseList(sysEnterprise);
        for (SysEnterprise sysEnterpris : sysEnterprises) {
            sysEnterpris.setStatus(EnterpriseStatusEnum.getStatusName(sysEnterpris.getStatus()));
        }
        return sysEnterprises;
    }

    /**
     * 新增企业
     * 
     * @param sysEnterprise 企业
     * @return 结果
     */
    @Override
    public int insertSysEnterprise(SysEnterprise sysEnterprise)
    {
        sysEnterprise.setCreateTime(DateUtils.getNowDate());
        sysEnterprise.setUserId(SecurityUtils.getUserId());
        sysEnterprise.setCreateBy(SecurityUtils.getLoginUser().getUser().getNickName());
        sysEnterprise.setStatus(EnterpriseStatusEnum.ENTERPRISE_APPLICATION.getCode());
        return sysEnterpriseMapper.insert(sysEnterprise);
    }

    /**
     * 修改企业
     * 
     * @param sysEnterprise 企业
     * @return 结果
     */
    @Override
    public int updateSysEnterprise(SysEnterprise sysEnterprise)
    {
        sysEnterprise.setUpdateTime(DateUtils.getNowDate());
        return sysEnterpriseMapper.updateSysEnterprise(sysEnterprise);
    }

    /**
     * 批量删除企业
     * 
     * @param ids 需要删除的企业主键
     * @return 结果
     */
    @Override
    public int deleteSysEnterpriseByIds(Long[] ids)
    {
        return sysEnterpriseMapper.deleteSysEnterpriseByIds(ids);
    }

    /**
     * 删除企业信息
     * 
     * @param id 企业主键
     * @return 结果
     */
    @Override
    public int deleteSysEnterpriseById(Long id)
    {
        return sysEnterpriseMapper.deleteSysEnterpriseById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean changeEnterpriseStatus(SysEnterprise enterprise) {
        if (ObjectUtils.isEmpty(enterprise)) {
            throw new ServiceException("企业信息不能为空!") ;
        }
        if (ObjectUtils.isEmpty(enterprise.getId())) {
            throw new ServiceException("企业编号不能为空!") ;
        }
        SysEnterprise sysEnterprise = this.getById(enterprise.getId());
        if (ObjectUtils.isEmpty(sysEnterprise)) {
            throw new ServiceException("企业编号不存在!") ;
        }
        LambdaUpdateWrapper<SysEnterprise> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.set(SysEnterprise::getStatus ,sysEnterprise.getStatus()) ;
        lambdaUpdateWrapper.eq(SysEnterprise::getId ,sysEnterprise.getId()) ;
        this.update(lambdaUpdateWrapper) ;
        /**修改企业岗位状态*/
        if (sysEnterprise.getStatus().equals(EnterpriseStatusEnum.ENTERPRISE_OFFLINE.getCode())) {
            LambdaQueryWrapper<SysPosition> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SysPosition::getEnterpriseId ,sysEnterprise.getId()) ;
            List<SysPosition> positionList = positionService.list(wrapper);
            if (CollectionUtils.isNotEmpty(positionList)) {
                LambdaUpdateWrapper<SysPosition> updateWrapper = new LambdaUpdateWrapper<>();
                updateWrapper.eq(SysPosition::getEnterpriseId ,sysEnterprise.getId()) ;
                updateWrapper.set(SysPosition::getStatus , PositionStatusEnum.POSITION_OFFLINE.getCode()) ;
                this.positionService.update(updateWrapper) ;
            }
        }

        return true;
    }

    @Override
    public boolean applicationStatus(SysEnterprise enterprise) {
        if (ObjectUtils.isEmpty(enterprise)) {
            throw new ServiceException("企业信息不能为空!") ;
        }
        if (ObjectUtils.isEmpty(enterprise.getId())) {
            throw new ServiceException("企业编号不能为空!") ;
        }
        if (StringUtils.isEmpty(enterprise.getStatus())){
            throw new ServiceException("企业状态不能为空!") ;
        }
        SysEnterprise sysEnterprise = this.getById(enterprise.getId());
        if (ObjectUtils.isEmpty(sysEnterprise)) {
            throw new ServiceException("企业编号不存在!") ;
        }
        LambdaUpdateWrapper<SysEnterprise> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(SysEnterprise::getId ,enterprise.getId()) ;
        wrapper.set(SysEnterprise::getStatus ,enterprise.getStatus()) ;
        return this.update(wrapper);
    }
}
